package com.caigou.modules.data.ui.law

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.data.R
import com.caigou.modules.data.adpter.LawItemAdapter
import com.caigou.modules.data.bean.LawItemBean
import com.caigou.modules.data.databinding.ActivityLawBinding
import com.caigou.modules.data.viewmodel.LawViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/29
 */
@Route(path = RouterPath.Data.PAGE_LAW)
@AndroidEntryPoint
class LawActivity : BaseActivity() {

    private lateinit var binding: ActivityLawBinding
    private val lawViewModel: LawViewModel by viewModels()

    private val mAdapter: LawItemAdapter by lazy {
        LawItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLawBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.lawLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        binding.lawSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchLawList()
            }
        }
        binding.lawRecyclerView.run {
            layoutManager = LinearLayoutManager(this@LawActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@LawActivity))
        }

        mAdapter.setOnItemClickListener { _, view, position ->
            val bean = mAdapter.getItem(position)
            ARouter.getInstance().build(RouterPath.Data.PAGE_LAW_DETAIL)
                .withInt(AppConstants.DATA_ID, bean.formulaId)
                .navigation()
        }

        binding.lawTvBack.setOnClickListener {
            finish()
        }

        fetchLawList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(lawViewModel.lawListLiveData, ::handleGetLawList)
    }

    private fun handleGetLawList(result: Resource<MutableList<LawItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.lawSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.lawSwipeRefreshLayout.isRefreshing = false
                result.data?.apply {
                    mAdapter.setList(this)
                }
            }
        }
    }

    private fun fetchLawList() {
        lawViewModel.getLawList()
    }
}